package org.jeecg.modules.analysis.genealogy.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.analysis.genealogy.entity.PatentGraphData;
import org.jeecg.modules.analysis.genealogy.entity.PatentNodeDetail;
import org.jeecg.modules.analysis.genealogy.service.IPatentGraphService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Description: 专利谱系图控制器
 * @Author: jeecg-boot
 * @Date: 2025-10-11
 * @Version: V1.0
 */
@Tag(name = "专利谱系图")
@RestController
@RequestMapping("/genealogy/patentGraph")
@Slf4j
public class PatentGraphController {

    @Autowired
    private IPatentGraphService patentGraphService;

    /**
     * 获取专利谱系图数据
     *
     * @param patentId 专利ID
     * @param relationType 关联类型: institution-机构关联, inventor-发明人关联
     * @return 谱系图数据
     */
    @Operation(summary = "获取专利谱系图数据")
    @GetMapping(value = "/getGraphData")
    public Result<PatentGraphData> getGraphData(
            @RequestParam(name = "patentId", required = true) String patentId,
            @RequestParam(name = "relationType", defaultValue = "institution") String relationType) {
        try {
            log.info("获取专利谱系图数据请求，专利ID: {}, 关联类型: {}", patentId, relationType);
            PatentGraphData graphData = patentGraphService.getPatentGraphData(patentId, relationType);
            return Result.OK(graphData);
        } catch (Exception e) {
            log.error("获取专利谱系图数据失败", e);
            return Result.error("获取专利谱系图数据失败: " + e.getMessage());
        }
    }

    /**
     * 计算专利重要性评分
     *
     * @param patentId 专利ID
     * @return 重要性评分
     */
    @Operation(summary = "计算专利重要性评分")
    @GetMapping(value = "/calculateImportance")
    public Result<Double> calculateImportance(@RequestParam(name = "patentId", required = true) String patentId) {
        try {
            log.info("计算专利重要性评分请求，专利ID: {}", patentId);
            Double importance = patentGraphService.calculatePatentImportance(patentId);
            return Result.OK(importance);
        } catch (Exception e) {
            log.error("计算专利重要性评分失败", e);
            return Result.error("计算专利重要性评分失败: " + e.getMessage());
        }
    }

    /**
     * 获取专利详细信息
     *
     * @param patentId 专利ID
     * @return 专利详细信息
     */
    @Operation(summary = "获取专利详细信息")
    @GetMapping(value = "/getPatentDetail")
    public Result<PatentNodeDetail> getPatentDetail(@RequestParam(name = "patentId", required = true) String patentId) {
        try {
            log.info("获取专利详细信息请求，专利ID: {}", patentId);
            PatentNodeDetail detail = patentGraphService.getPatentDetail(patentId);
            if (detail == null) {
                return Result.error("未找到该专利信息");
            }
            return Result.OK(detail);
        } catch (Exception e) {
            log.error("获取专利详细信息失败", e);
            return Result.error("获取专利详细信息失败: " + e.getMessage());
        }
    }
}


